<?php
/* 
* RandomContent 1.0
* Resource Type: Snippet
* Author: atma
* Description: Display a random content from a pre-defined folder.
* Dated 2009-02-20
* Use: insert [!RandomContent? &folder=`1234` &num=`1`!] where &folder is your document container ID, &num is number of displayed docs.
*/

$output = '';
isset($num) ? $num : 1;
if (!$folder) {
    $output .= 'Parent folder not defined or not found';
    return $output;
}

$resource = $modx->getActiveChildren($folder,'createdon', 'DESC', $fields='content');
$quantity = count($resource);
if ($quantity < $num) {
    $output .= 'Number of random chunks more than children docs';
    return $output;
}
if($quantity > 0) {
    if($quantity == 1) {
        $output .= '<div>';
        $output .= $resource[0]['content'];
        $output .= '</div>';
    } else {
        if($num == 1) {
            $result = array(array_rand($resource, $num));
        }
        else {
            $result = array_rand($resource, $num);
        } 
        
        for ($i=0; $i < count($result); $i++) {
            $output .= '<div>';
            $output .= $resource[$result[$i]]['content'];
            //$output .= $result[$i]['content'];
            $output .= '</div>';
        }
    }
} else {
    $output .= 'No children documents found';
} 

return $output;
?>
